package webservice;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Description: WebService  客户端
 * @Project: testDemo
 * @Author: hjibo
 * @CreateDate: 2019-03-20 11:25
 * @ModificationHistory:
 */
public class WebServiceClient {

    public static void main(String[] args) throws IOException {

//        String url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
//        String soap = "" +
//                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//                "  <soap:Body>\n" +
//                "    <qqCheckOnline xmlns=\"http://WebXml.com.cn/\">\n" +
//                "      <qqCode>1013130430</qqCode>\n" +
//                "    </qqCheckOnline>\n" +
//                "  </soap:Body>\n" +
//                "</soap:Envelope>";

//        String url = "http://192.168.1.12/corp.asmx";
//        String soap = "" +
//                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//                "  <soap:Body>\n" +
//                "    <GetSession xmlns=\"http://tempuri.org/\">\n" +
//                "      <sessionid>1</sessionid>\n" +
//                "      <stype>2</stype>\n" +
//                "      <secret>2</secret>\n" +
//                "      <code>4</code>\n" +
//                "    </GetSession>\n" +
//                "  </soap:Body>\n" +
//                "</soap:Envelope>";
//        String soap = "" +
//                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//                "  <soap:Body>\n" +
//                "    <SendMessage xmlns=\"http://tempuri.org/\">\n" +
//                "      <psncode>11</psncode>\n" +
//                "      <stype>22</stype>\n" +
//                "      <secret>33</secret>\n" +
//                "    </SendMessage>\n" +
//                "  </soap:Body>\n" +
//                "</soap:Envelope>";

        String url = "http://192.168.2.125:8888/corp.asmx";
        String soap = "" +
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "  <soap:Body>\n" +
                "    <sayhello xmlns=\"http://webservice/\">\n" +
                "      <arg0 xmlns=\"\">黄继波</arg0>\n" +
                "    </sayhello>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";

        // 服务端地址
        URL webServiceURL = new URL(url);
        // 打开连接
        HttpURLConnection conn = (HttpURLConnection) webServiceURL.openConnection();
        // 设置连接属性
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        // 获取连接输出流
        OutputStream os = conn.getOutputStream();
        os.write(soap.getBytes());
        // 获取连接输入流
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder content = new StringBuilder();
        String s;
        while((s = br.readLine()) != null){
            content.append(s);
            content.append("\r\n");
        }
        br.close();
        os.close();
        is.close();
        conn.disconnect();

        // 输出到控制台
        System.out.print("content:\n"+content);
    }
}
